﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using FoundationClass.Entity;

namespace FoundationClass.Data
{
    public static class PromotionData
    {
        public static void InsertPromotion(Promotion promotion)
        {
            DataTable promotionDt = DatabaseManager.Read("Promotion");
            int newId = GetMaxPromotionId() + 1;
            promotionDt.Rows.Add(newId, promotion.Name, promotion.StartDate, promotion.EndDate, promotion.StartTime, promotion.EndTime, promotion.Type, promotion.PriceRate, promotion.FixHour,promotion.Detail,promotion.IsTotal);
            DatabaseManager.Write(promotionDt);

        }

        private static int GetMaxPromotionId()
        {
            DataTable promotionDt = DatabaseManager.Read("Promotion");
            int rowcount = promotionDt.Rows.Count;
            if (rowcount == 0) 
                return 1;
            return int.Parse(promotionDt.Rows[rowcount - 1]["id"].ToString());
        }

        public static void UpdatePromotion(Promotion promotion)
        {
            DataTable promotionDt = DatabaseManager.Read("Promotion");
            foreach (DataRow row in promotionDt.Rows)
            {
                if (row["id"].Equals(promotion.Id.ToString()))
                {
                    row["name"] = promotion.Name;
                    row["startdate"] = promotion.StartDate;
                    row["enddate"] = promotion.EndDate;
                    row["starttime"] = promotion.StartTime;
                    row["endtime"] = promotion.EndTime;
                    row["type"] = promotion.Type;
                    row["pricerate"] = promotion.PriceRate;
                    row["fixhour"] = promotion.FixHour;
                    row["detail"] = promotion.Detail;
                    row["istotal"] = promotion.IsTotal;
                    break;
                }
            }
            DatabaseManager.Write(promotionDt);
        }

        public static void DeletePromotion(int id)
        {
            DataTable promotionDt = DatabaseManager.Read("Promotion");
            foreach (DataRow row in promotionDt.Rows)
            {
                if (row["id"].Equals(id.ToString()))
                {
                    row.Delete();
                    break;
                }
            }
            DatabaseManager.Write(promotionDt);
        }

        public static Promotion GetPromotion(int id)
        {
            Promotion promotion = null;
            DataTable promotionDt = DatabaseManager.Read("Promotion");
            foreach (DataRow row in promotionDt.Rows)
            {
                if ((row["id"]).Equals(id.ToString()))
                {
                    promotion = new Promotion()
                    {
                        Id = int.Parse(row["id"].ToString()),
                        Name = (string)row["name"],
                        StartDate = string.IsNullOrEmpty(row["startdate"] as string) ? DateTime.Now : DateTime.Parse(row["startdate"].ToString()),
                        EndDate = string.IsNullOrEmpty(row["enddate"] as string) ? DateTime.Now : DateTime.Parse(row["enddate"].ToString()),
                        StartTime = string.IsNullOrEmpty(row["starttime"] as string) ? DateTime.Now : DateTime.Parse(row["starttime"].ToString()),
                        EndTime = string.IsNullOrEmpty(row["endtime"] as string) ? DateTime.Now : DateTime.Parse(row["endtime"].ToString()),
                        Type = int.Parse(row["type"].ToString()),
                        PriceRate = double.Parse(row["pricerate"].ToString()),
                        FixHour = int.Parse(row["fixhour"].ToString()),
                        Detail = (string)row["detail"],
                        IsTotal =  bool.Parse(row["istotal"].ToString())
                    };
                    break;
                }
            }
            return promotion;
        }

        public static IList<Promotion> GetAllPromotions()
        {
            try
            {
                IList<Promotion> promotions = null;
                DataTable promotionDt = DatabaseManager.Read("Promotion");
                if (promotionDt.Rows.Count > 0)
                {
                    promotions = new List<Promotion>();
                    foreach (DataRow row in promotionDt.Rows)
                    {
                        promotions.Add(new Promotion
                        {
                            Id = int.Parse(row["id"].ToString()),
                            Name = (string)row["name"],
                            StartDate = string.IsNullOrEmpty(row["startdate"] as string) ? DateTime.Now : DateTime.Parse(row["startdate"].ToString()),
                            EndDate = string.IsNullOrEmpty(row["enddate"] as string) ? DateTime.Now : DateTime.Parse(row["enddate"].ToString()),
                            StartTime = string.IsNullOrEmpty(row["starttime"] as string) ? DateTime.Now : DateTime.Parse(row["starttime"].ToString()),
                            EndTime = string.IsNullOrEmpty(row["endtime"] as string) ? DateTime.Now : DateTime.Parse(row["endtime"].ToString()),
                            Type = int.Parse(row["type"].ToString()),
                            PriceRate = double.Parse(row["pricerate"].ToString()),
                            FixHour = int.Parse(row["fixhour"].ToString()),
                            Detail = (string)row["detail"],
                            IsTotal = bool.Parse(row["istotal"].ToString())
                        });
                    }
                }
                return promotions;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static IList<Promotion> GetPromotionsByType(int type)
        {
            IList<Promotion> promotions = null;
            DataTable promotionDt = DatabaseManager.Read("Promotion");
            if (promotionDt.Rows.Count > 0)
            {
                promotions = new List<Promotion>();
                foreach (DataRow row in promotionDt.Rows)
                {
                    if (row["type"].Equals(type.ToString()))
                    {
                        promotions.Add(new Promotion
                                           {
                                               Id = int.Parse(row["id"].ToString()),
                                               Name = (string)row["name"],
                                               StartDate = string.IsNullOrEmpty(row["startdate"] as string) ? DateTime.Now : DateTime.Parse(row["startdate"].ToString()),
                                               EndDate = string.IsNullOrEmpty(row["enddate"] as string) ? DateTime.Now : DateTime.Parse(row["enddate"].ToString()),
                                               StartTime = string.IsNullOrEmpty(row["starttime"] as string) ? DateTime.Now : DateTime.Parse(row["starttime"].ToString()),
                                               EndTime = string.IsNullOrEmpty(row["endtime"] as string) ? DateTime.Now : DateTime.Parse(row["endtime"].ToString()),
                                               Type = int.Parse(row["type"].ToString()),
                                               PriceRate = double.Parse(row["pricerate"].ToString()),
                                               FixHour = int.Parse(row["fixhour"].ToString()),
                                               Detail = (string)row["detail"],
                                               IsTotal = bool.Parse(row["istotal"].ToString())
                                           });
                    }
                }
            }
            return promotions;
        }
    }
}
